package testrank.affinity;

import java.util.List;
import java.util.StringTokenizer;

/**
 * @author Hagai Cibulski
 *
 */
public class SignatureParser {

	private String signature;
	
	private String methodNameLiteral;
	
	private List<String> methodNameWords;

	private String classNameLiteral;
	
	private List<String> classNameWords;
	
	
	public SignatureParser(String signature) {
		this.signature = signature;
		StringTokenizer st = new StringTokenizer(signature, " ()", true);
		String t1 = st.nextToken();
		String t2 = st.nextToken();

		boolean ctor;
		String fullyQualifiedMethodName;
		if (t2.equals(" ")) {
			fullyQualifiedMethodName = st.nextToken();
			ctor = false;
		}
		else {	//ctor
			// p.C() --> <None> p.C.<init>()
			//fullyQualifiedMethodName = t1 + ".<init>";
			
			// p.C() --> <None> p.C()
			fullyQualifiedMethodName = t1;
			ctor = true;
		}
		
		String[] parts = fullyQualifiedMethodName.split("\\.");
		
		methodNameLiteral = parts[parts.length - 1];
		methodNameWords = SourceFileAnalyzer.getIdentiferParts(methodNameLiteral);
		if (ctor) { //Character.isUpperCase(methodNameLiteral.charAt(0))) {
			classNameLiteral = methodNameLiteral; 
			classNameWords = methodNameWords;
		}
		else {
			classNameLiteral = parts[parts.length - 2]; 
			classNameWords = SourceFileAnalyzer.getIdentiferParts(classNameLiteral);
		}
	}

	public String getSignature() {
		return this.signature;
	}

	public String getMethodNameLiteral() {
		return this.methodNameLiteral;
	}

	public List<String> getMethodNameWords() {
		return this.methodNameWords;
	}
	
	public String getClassNameLiteral() {
		return this.classNameLiteral;
	}

	public List<String> getClassNameWords() {
		return this.classNameWords;
	}

	public static void main(String[] args) {
		new SignatureParser("ReturnType com.company.product.module.TheClass.theMethod(String arg0, List<String> theList)");
	}
}
